package com.wyl.servlet

import com.wyl.pojo.User
import com.wyl.service.role.RoleService
import com.wyl.service.role.RoleServiceImpl
import com.wyl.service.user.UserService
import com.wyl.service.user.UserServiceImpl
import com.wyl.utils.MESSAGE
import com.wyl.utils.USER_SESSION
import javax.servlet.annotation.WebServlet
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

@WebServlet("/jsp/user.do")
class UserServlet : HttpServlet() {
    private val userService: UserService = UserServiceImpl()
    private val roleService: RoleService = RoleServiceImpl()

    override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
        this.doGet(req, resp)
    }

    override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
        when (req.getParameter("method")) {
            "savepwd" -> savePwd(req, resp)
            "pwdmodify" -> modifyPwd(req, resp)
            "query" -> query(req, resp)
            else -> {
            }
        }
    }

    private fun query(req: HttpServletRequest, resp: HttpServletResponse) {
        val queryUserName: String? = req.getParameter("queryname")
        val queryUserRole = req.getParameter("queryUserRole")?.toIntOrNull()
        val currentPageNo = req.getParameter("pageIndex")?.toIntOrNull()

        val totalCount = userService.getUserCount(queryUserName, queryUserRole)
        val pageSize = 5
        val totalPageCount = (totalCount / pageSize) + 1
        val currentPage = when {
            currentPageNo == null -> 1
            currentPageNo < 1 -> 1
            currentPageNo > totalPageCount -> totalPageCount
            else -> currentPageNo
        }

        val userList = userService.getUserList(queryUserName, queryUserRole, currentPage, pageSize)
        val roleList = roleService.getRoleList()

        println("userList = $userList")
        println("roleList = $roleList")
        println("totalCount = $totalCount")
        println("currentPageNo = $currentPageNo")
        println("currentPage = $currentPage")
        println("totalPageCount = $totalPageCount")
        println("queryUserName = $queryUserName")
        println("queryUserRole = $queryUserRole")

        req.setAttribute("userList", userList)
        req.setAttribute("roleList", roleList)
        req.setAttribute("totalCount", totalCount)
        req.setAttribute("currentPageNo", currentPage)
        req.setAttribute("totalPageCount", totalPageCount)
        req.setAttribute("queryUserName", queryUserName)
        req.setAttribute("queryUserRole", queryUserRole)
        req.getRequestDispatcher("userlist.jsp").forward(req, resp)
    }

    private fun modifyPwd(req: HttpServletRequest, resp: HttpServletResponse) {
        val pwd: String? = req.getParameter("oldpassword")
        val user = req.session.getAttribute(USER_SESSION) as User?
        val result = if (user != null && !pwd.isNullOrEmpty()) {
            if (userService.login(user.userCode, pwd) != null)
                "true"
            else
                "false"
        } else
            "error"
//        resp.contentType = "application/json;charset=utf-8"
        resp.writer.use {
            val print = "{\"result\": $result}"
            println(print)
            it.write(print)
            it.flush()
        }
    }

    private fun savePwd(req: HttpServletRequest, resp: HttpServletResponse) {
        val pwd: String? = req.getParameter("newpassword")
        val user = req.session.getAttribute(USER_SESSION) as User?
        if (user != null && !pwd.isNullOrEmpty()) {
            if (userService.updatePwd(user.id, pwd)) {
                req.setAttribute(MESSAGE, "密码修改成功, 请退出, 使用新密码登录")
                req.removeAttribute(USER_SESSION)
            } else {
                req.setAttribute(MESSAGE, "密码修改失败")
            }
        } else {
            req.setAttribute(MESSAGE, "新密码设置错误")
        }
        req.getRequestDispatcher("pwdmodify.jsp").forward(req, resp)
    }

}